package com.sichard.waterfullview.request.view;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.sichard.waterfullview.R;
import com.sichard.waterfullview.common.mvp.MvpBaseActivity;
import com.sichard.waterfullview.request.RequestContract;
import com.sichard.waterfullview.request.data.WaterfallEntity;
import com.sichard.waterfullview.request.presenter.RequestPresenter;

import java.util.List;

/**
 * <br>类表述：
 * <br>详细描述：
 * <br><b>Author Caoshichao </b></br>
 * <br><b>Date  2020/4/15</b></br>
 */
public class WaterfallActivity extends MvpBaseActivity<RequestPresenter> implements RequestContract.View {
    private RecyclerView mRecyclerView;
    private RecyclerViewAdapter mAdapter;
    private ProgressBar mProgressBar;
    private TextView mHintText;
    private RecyclerViewUtil mRecyclerViewUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water_full);
        initView();
        requestWaterfallData();
    }

    private void initView() {
        mProgressBar = findViewById(R.id.progressBar);
        mHintText = findViewById(R.id.text_hint);
        mRecyclerView = findViewById(R.id.recyclerView);


        int spanCount = 2;
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new RecyclerViewAdapter(this);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerViewUtil = new RecyclerViewUtil(mRecyclerView);
        mRecyclerViewUtil.setRecyclerViewLoadMoreListener(new RecyclerViewUtil.RecyclerViewLoadMoreListener() {
            @Override
            public void onLoadMore() {
                Toast.makeText(getApplicationContext(), R.string.hint_loading_more, Toast.LENGTH_SHORT).show();
                mRecyclerViewUtil.setLoadMoreEnable(false);
                requestWaterfallData();
            }
        });
        mRecyclerViewUtil.setLoadMoreEnable(true);
    }

    @Override
    protected RequestPresenter createPresenter() {
        return new RequestPresenter();
    }

    @Override
    public void requestWaterfallData() {
        mPresenter.requestWaterfallData();
    }

    @Override
    public void showResult(List<WaterfallEntity.HitsEntity> result) {
        mRecyclerView.setVisibility(View.VISIBLE);
        mHintText.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.GONE);
        mAdapter.addData(result);
        mRecyclerViewUtil.setLoadMoreEnable(true);
    }

    @Override
    public void showError(String errMsg) {
        mProgressBar.setVisibility(View.GONE);
        mHintText.setText(errMsg);
        mRecyclerViewUtil.setLoadMoreEnable(true);
    }
}
